<?php
class Movie_Model extends Model {
	
	function __construct()
	{
		parent::Model();
	}
	
	function insert($data)
	{
		$this->db->like('movie_name', $data['movie_name']);
		$this->db->from('movies');
		$count = $this->db->count_all_results();
		if($count == 0 )
		$result = $this->db->insert('movies', $data);
		else 
		$result = 10;
		return $result; 
	}
	function get_movies($id="",$limit="")
	{
		$this->db->select('*');
		if($limit!='')
		$this->db->limit($limit);
		if($id!="")
		$this->db->where('id',$id);
		$resultset = $this->db->get('movies');
		if($id!="")
		return $resultset->row();
		else 
		return $resultset->result();
	}
	
	// to get the movie names if trailors details exists
	function get_movies_byorder($ascii=77,$orderby=false,$limit=array()){
		
		$query = "SELECT distinct(movies.id),movies.movie_name FROM
						    trailors
						    INNER JOIN movies 
						    ON (trailors.movie_id = movies.id)";
		if($ascii!="" && $orderby == false)
		$query .= "where upper(movie_name) like '".chr($ascii)."%'";
		if($ascii=="" && $orderby == true)
		$query .= " order by movies.id desc";
		if(!empty($limit))
		{
			list($start,$end) = $limit;
			$query .= " limit $start,$end";
		}
		$resultset = $this->db->query($query);
//		$this->db->select('id,movie_name');
//		$this->db->like('upper(movie_name)', chr($ascii), 'after');
//		$this->db->order_by("movie_name"); 
//		$resultset = $this->db->get('movies');		
		return $resultset->result();
	}
	function  get_recent_movies(){
		$this->db->order_by("insert_date", "desc"); 
		$result=$this->db->get('movies');

		return $result->result();
	}
	function get_trailer_id(){
		$query = $this->db->get('trailors');
		$query->result();
	}
	
	  
}
?>